#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <iostream>

void ChildRead(int fd)
{
    char msg[1024]; msg[0] = '\0';
    ssize_t s = read(fd, msg, sizeof(msg));
    if(s > 0)
    {
        msg[s] = '\0';
        std::cout << msg << std::endl;
    }
}

void FatherWrite(int fd)
{
    char msg[1024] = "I am father";
    write(fd, msg, strlen(msg));
}

int main()
{
    int pipefd[2];
    int ret = pipe(pipefd); if(ret < 0) { perror("pipe err"); exit(1); }
    pid_t id = fork();
    if(id < 0) { perror("fork err"); exit(1); }
    else if(id == 0)
    {
        close(pipefd[1]);
        ChildRead(pipefd[0]);
    }
    else
    {
        close(pipefd[0]);
        FatherWrite(pipefd[1]);
        waitpid(id, nullptr, 0);
    }
    return 0;
}